// Copyright (c) 1996-2002 John Lyon-Smith. All rights reserved.

#pragma once

#include <wxtl\base.h>
#include <wxtl\path.h>
#include <stdio.h>
#include <stdarg.h>
#include <crtdbg.h>

namespace wxtl
{
	#ifdef _DEBUG

	inline void _cdecl trace(const char* pszFormat, ...)
	{
		va_list ptr;
		va_start(ptr, pszFormat);
		static const int count = 1023;
		char buf[count + 1] = {'\0'};

		_vsnprintf_s(buf,count + 1, count, pszFormat, ptr);

		_CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%s", buf);

		va_end(ptr);
	}

	inline void _cdecl traceLine(
		const char* fileName, 
		int lineNo, 
		const CHAR* format, ...)
	{
		va_list ptr;
		va_start(ptr, format);
		static const int count = 1023;
		char buf[count + 1] = {'\0'};

		_vsnprintf_s(buf,count + 1, count, format, ptr);

		char modBuf[WXTL_BIG_MAX_PATH];
		
		::GetModuleFileNameA(NULL, modBuf, WXTL_BIG_MAX_PATH);
		
		const char* modName = strrchr(modBuf, '\\') + sizeof(char);

		_CrtDbgReport(_CRT_WARN, fileName, lineNo, modName, "%s\n", buf);

		va_end(ptr);
	}

	#define WXTRACE			wxtl::trace
	#define WXTRACELINE		wxtl::traceLine

	#define WXTRACELINE0(f)				wxtl::traceLine(__FILE__, __LINE__, f)
	#define WXTRACELINE1(f, a1)			wxtl::traceLine(__FILE__, __LINE__, f, a1)
	#define WXTRACELINE2(f, a1, a2)		wxtl::traceLine(__FILE__, __LINE__, f, a1, a2)
	#define WXTRACELINE3(f, a1, a2, a3)	wxtl::traceLine(__FILE__, __LINE__, f, a1, a2, a3)

	#else //_DEBUG

	inline void _cdecl trace(LPCSTR , ...){}
	inline void _cdecl traceLine(const TCHAR*, int, LPCSTR , ...){}
	inline void _cdecl trace(LPCWSTR , ...){}
	inline void _cdecl traceLine(const TCHAR*, int, LPCWSTR , ...){}

	#define WXTRACE			__noop
	#define WXTRACELINE		__noop
	#define WXTRACELINE0	__noop
	#define WXTRACELINE1	__noop
	#define WXTRACELINE2	__noop
	#define WXTRACELINE3	__noop

	#endif //_DEBUG

	// This macro is based on sample code found at http://www.codeproject.com/macro/location_pragma.asp
	//
	// Usage: #pragma message(WXCLICK("Something to display during compilation.\n"))
	
	#define __WXCLICK_ARG_2__(ArgString)  #ArgString
	#define __WXCLICK_ARG_1__(ArgString)  __WXCLICK_ARG_2__(ArgString)
	#define __WXCLICK_LOCATION__  __FILE__ "("__WXCLICK_ARG_1__(__LINE__)") : "
	#define WXCLICK(DoubleQuotedString)  __WXCLICK_LOCATION__ DoubleQuotedString
}
